package com.i2works.smartluolong.manage.service.admin.impl;

import com.google.common.collect.Lists;
import com.i2works.smartluolong.manage.dal.admin.dao.IAdminResDAO;
import com.i2works.smartluolong.manage.dal.admin.dao.IAdminRoleDAO;
import com.i2works.smartluolong.manage.entity.admin.AdminResDO;
import com.i2works.smartluolong.manage.entity.admin.AdminResQuery;
import com.i2works.smartluolong.manage.entity.admin.AdminRoleResDO;
import com.i2works.smartluolong.manage.service.admin.IAdminResService;
import com.i2works.smartluolong.utils.base.GlobalConstant;
import com.i2works.smartluolong.utils.basecommon.BaseCommonServiceImpl;
import com.i2works.smartluolong.utils.exception.DAOException;
import com.i2works.smartluolong.utils.exception.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.Set;

/**
 * 管理员资源表实现类
 *
 * @author koal <koal@vip.qq.com>
 * @ClassName: AdminResServiceImpl

 * @date 2015年11月3日 下午11:38:38
 */
@Service("adminResService")
public class AdminResServiceImpl extends BaseCommonServiceImpl<AdminResQuery,AdminResDO> implements IAdminResService {

    @Autowired
    private IAdminResDAO adminResDAO;

    @Autowired
    private IAdminRoleDAO adminRoleDAO;


    /**
     * 根据查询资源列表(树)
     */
    @Override
    public List<AdminResDO> queryTreeList(AdminResQuery query) throws ServiceException {
        try {
            return adminResDAO.queryTreeList(query);
        } catch (DAOException e) {
            throw new ServiceException(e);
        }
    }


    /**
     * 查询某个用户的资源
     */
    @Override
    public Set<AdminResDO> queryUserResList(AdminResQuery query) throws ServiceException {
        try {
            return adminResDAO.queryUserResList(query);
        } catch (DAOException e) {
            throw new ServiceException(e);
        }
    }

    /**
     * 添加系统资源
     */
    @Override
    public void save(AdminResDO adminResDO) throws ServiceException {
        try {
            if (adminResDO.getId() != null && adminResDO.getId() != 0) {
                adminResDAO.update(adminResDO);
            } else {
                adminResDO.setCreateName(adminResDO.getUpdateName());
                adminResDAO.save(adminResDO);
            }
        } catch (DAOException e) {
            throw new ServiceException(e);
        }
    }



    /**
     * 返回某人所拥有的资源ID
     */
    @Override
    public AdminResDO queryHasChecked(AdminResQuery query) throws ServiceException {
        try {
            return adminResDAO.queryHasChecked(query);
        } catch (DAOException e) {
            throw new ServiceException(e);
        }
    }

    private void updateRoleResStatus(AdminResDO adminResDO) {
        try {
            //关联设置其角色与资源表状态
            AdminRoleResDO adminRoleResDO = new AdminRoleResDO();
            adminRoleResDO.setUpdateName(adminResDO.getUpdateName());
            adminRoleResDO.setResId(adminResDO.getId());
            adminRoleResDO.setStatus(adminResDO.getStatus());
            adminRoleDAO.updateRoleResStatus(adminRoleResDO);
        } catch (DAOException e) {
            e.printStackTrace();
        }
    }


    @Override
    public void updateStatus(AdminResDO adminResDO) throws ServiceException {
        try {
            if (null != adminResDO) {
                adminResDAO.update(adminResDO);
                //如果不是按钮级别类型，即可说明用于子类，然后关联设置其子类状态
                if (!Objects.equals(adminResDO.getResType(), GlobalConstant.resType.T3)) {
                    AdminResQuery query = new AdminResQuery();
                    query.setPid(adminResDO.getId());
                    //查询其所有子类
                    List<AdminResDO> childrenResList = adminResDAO.queryAllList(query);
                    //循环输出子类，并设置其status为父类status
                    for (AdminResDO childrenRes : childrenResList) {
                        childrenRes.setUpdateName(adminResDO.getUpdateName());
                        childrenRes.setStatus(adminResDO.getStatus());
                        //更新子类
                        adminResDAO.update(childrenRes);
                        //更新子类的角色与资源表状态
                        updateRoleResStatus(childrenRes);
                    }
                }
                //更新角色与资源表状态
                updateRoleResStatus(adminResDO);
            }
        } catch (DAOException e) {
            throw new ServiceException(e);
        }
    }


    @Override
    public List<AdminResDO> querySelect(AdminResQuery query) throws ServiceException {
        try {
            List<AdminResDO> resSelect = adminResDAO.queryList(query);
            List<AdminResDO> formatSelect = Lists.newArrayList();
            for (AdminResDO adminResDO : resSelect) {
                if(adminResDO.getResType() == 1){
                    formatSelect.add(adminResDO);
                    AdminResQuery adminResQuery = new AdminResQuery();
                    adminResQuery.setPid(adminResDO.getId());
                    adminResQuery.setResType(2);
                    List<AdminResDO> subResList = adminResDAO.queryList(adminResQuery);
                    if(subResList.size() > 0){
                        formatSelect.addAll(subResList);
                    }
                }
            }
            return formatSelect;
        } catch (DAOException e) {
            throw new ServiceException(e);
        }
    }



    @Override
    public void deleteBatch(List<Integer> idsList) throws ServiceException {
        try {
            adminResDAO.deleteBatch(idsList);
            //删除关联表
            adminResDAO.deleteBatchRoleRes(idsList);
        } catch (DAOException e) {
            throw new ServiceException(e);
        }
    }

    @Override
    public void delete(Integer id) throws ServiceException {
        try {
            adminResDAO.delete(id);
            //删除关联表
            adminResDAO.deleteRoleRes(id);
        } catch (DAOException e) {
            throw new ServiceException(e);
        }
    }
}
